using System.Collections.Generic;
using UnityEngine;

public class ControlsPositionsSaver : MonoBehaviour
{
	public static ControlsPositionsSaver instance;

	private ButtonPositionSaver[] buttons;

	public UISlider sliderScale;

	public UIWidget leftButtonsWidget;

	public UIWidget rightButtonsWidget;

	public ButtonSwitcher buttonSwitcher;

	public Color selectedColor;

	private ButtonPositionSaver curButton;

	private bool LeftHandControl;

	[SerializeField]
	private SelectableButtonController _tabSettings;

	[SerializeField]
	private SelectableButtonController _tabCarSettings;

	[SerializeField]
	private SelectableButtonController _orientationButotns;

	[SerializeField]
	private GameObject[] _settinsControls;

	[SerializeField]
	private GameObject[] _settinsCarControls;

	private void Awake()
	{
		instance = this;
		buttons = GetComponentsInChildren<ButtonPositionSaver>(true);
		for (int i = 0; i < buttons.Length; i++)
		{
			buttons[i].master = this;
		}
	}

	private void OnEnable()
	{
		sliderScale.value = 0.5f;
		LeftHandControl = settings.LeftHandControlEnable;
		SetCurrentButton(null);
		_tabSettings.allButtons[0].Select(true);
		_tabCarSettings.allButtons[0].Select(true);
		if (LeftHandControl)
		{
			_orientationButotns.allButtons[0].Select(true);
			_orientationButotns.allButtons[1].Select(false);
		}
		else
		{
			_orientationButotns.allButtons[1].Select(true);
			_orientationButotns.allButtons[0].Select(false);
		}
	}

	private void Start()
	{
	}

	public void CancelSettings()
	{
		for (int i = 0; i < buttons.Length; i++)
		{
			buttons[i].CancelSettings();
		}
	}

	public void DefaultSettings()
	{
		LeftHandControl = false;
		_orientationButotns.allButtons[1].Select(true);
		_orientationButotns.allButtons[0].Select(false);
		for (int i = 0; i < buttons.Length; i++)
		{
			if (buttons[i].gameObject.GetActive())
			{
				buttons[i].DefaultSettings();
			}
		}
		SetCurrentButton(null);
	}

	private void SetOrientation(bool value)
	{
		for (int i = 0; i < buttons.Length; i++)
		{
			buttons[i].SetOrientation(LeftHandControl);
		}
	}

	public void SwithLeftOrientantion()
	{
		LeftHandControl = true;
		settings.LeftHandControlEnable = LeftHandControl;
		SetOrientation(LeftHandControl);
		buttonSwitcher.SwitchLeft();
	}

	public void SwitchRightOrientantion()
	{
		LeftHandControl = false;
		settings.LeftHandControlEnable = LeftHandControl;
		SetOrientation(LeftHandControl);
		buttonSwitcher.SwitchRight();
	}

	public void SwitchOrientation()
	{
		LeftHandControl = !LeftHandControl;
		SetOrientation(LeftHandControl);
		if (LeftHandControl)
		{
			buttonSwitcher.SwitchLeft();
		}
		else
		{
			buttonSwitcher.SwitchRight();
		}
	}

	public void SaveControls()
	{
		for (int i = 0; i < buttons.Length; i++)
		{
			buttons[i].SaveSettings();
		}
		settings.LeftHandControlEnable = LeftHandControl;
		PlayerPrefs.Save();
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Controls_Changes", "Custom_controls_saves");
		FlurryWrapper.LogEvent(FlurryWrapper.EV_CONTROLL_CHANGES, dictionary);
	}

	public void SetCurrentButton(ButtonPositionSaver button)
	{
		if (curButton != button)
		{
			if ((bool)curButton)
			{
				curButton.SetColor(Color.white);
			}
			curButton = button;
			if ((bool)curButton)
			{
				sliderScale.value = curButton.GetScale();
				curButton.SetColor(selectedColor);
			}
		}
		if (button == null)
		{
			sliderScale.alpha = 0.6f;
			sliderScale.enabled = false;
		}
		else
		{
			sliderScale.alpha = 1f;
			sliderScale.enabled = true;
		}
	}

	public void OnScaleChange()
	{
		if ((bool)curButton)
		{
			curButton.SetScale(sliderScale.value);
		}
	}

	public void ShowThis()
	{
		base.gameObject.SetActive(true);
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Controls_Changes", "Custom_controls_in");
		FlurryWrapper.LogEvent(FlurryWrapper.EV_CONTROLL_CHANGES, dictionary);
	}

	public void HideThis()
	{
		SetCurrentButton(null);
		if (controllerMenu.thisScript != null)
		{
			controllerMenu.thisScript.ShowSettings();
		}
		else if (GameController.instance != null)
		{
			PauseWindow.instance.ShowSettings();
		}
		base.gameObject.SetActive(false);
	}

	public void ShowSettingsControl(int count)
	{
		int num = 0;
		GameObject[] settinsControls = _settinsControls;
		foreach (GameObject gameObject in settinsControls)
		{
			gameObject.SetActive(num == count);
			num++;
		}
	}

	public void ShowSettingsCarControl(int count)
	{
		int num = 0;
		GameObject[] settinsCarControls = _settinsCarControls;
		foreach (GameObject gameObject in settinsCarControls)
		{
			gameObject.SetActive(num == count);
			num++;
		}
	}
}
